package com.ph.irp.base.verifyproject.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectCustomerService;
import com.ph.irp.dto.CustomerDTO;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.model.PBaseVerifyprojectCustomer;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class VerifyprojectCustomerAction extends ActionSupport implements
		ShowPageList {
	private String FORWARD;
	private PageDTO page;
	private String cpage;
	private PBaseVerifyprojectCustomerService customerService;
	private CustomerDTO cdto;
	private List<CustomerDTO> cdtos;
	private List<CustomerDTO> tempcdtos;
	private PBaseVerifyprojectCustomer verifyprojectCustomer;
	private Object[] rowlist = null;

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public void setCustomerService(
			PBaseVerifyprojectCustomerService customerService) {
		this.customerService = customerService;
	}

	public CustomerDTO getCdto() {
		return cdto;
	}

	public void setCdto(CustomerDTO cdto) {
		this.cdto = cdto;
	}

	public List<CustomerDTO> getCdtos() {
		return cdtos;
	}

	public void setCdtos(List<CustomerDTO> cdtos) {
		this.cdtos = cdtos;
	}

	public PBaseVerifyprojectCustomer getVerifyprojectCustomer() {
		return verifyprojectCustomer;
	}

	public void setVerifyprojectCustomer(
			PBaseVerifyprojectCustomer verifyprojectCustomer) {
		this.verifyprojectCustomer = verifyprojectCustomer;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public String showCustomerList() throws Throwable {
		verifyprojectCustomer = null;
		tempcdtos = customerService.getCustomerList(verifyprojectCustomer);
		if (tempcdtos != null) {
			if (tempcdtos.size() != 0) {
				cpage = "1";
				page = PageUtil.generatePageDTO(tempcdtos,
						"showCustomerPageList.action", cpage);
				cdtos = page.getResList();
			} else {
				cdtos = tempcdtos;
			}
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}
		return FORWARD;
	}

	public String showPageList() throws Throwable {
		if (tempcdtos != null) {
			page = PageUtil.generatePageDTO(tempcdtos,
					"showCustomerPageList.action", cpage);
			cdtos = page.getResList();
			return "success";
		} else {
			return "input";
		}
	}

	public String searchCustomer() throws Throwable {
		tempcdtos = customerService.getCustomerList(verifyprojectCustomer);
		
		if (tempcdtos != null) {
			if (tempcdtos.size() != 0) {
				cpage = "1";
				page = PageUtil.generatePageDTO(tempcdtos,
						"showCustomerPageList.action", cpage);
				cdtos = page.getResList();
			} else {
				cdtos = tempcdtos;
			}
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		
//		page = PageUtil.generatePageDTO(tempcdtos,
//				"showCustomerPageList.action", cpage);
//		cdtos = page.getResList();
		verifyprojectCustomer = null;
		return FORWARD;
	}

	// List页面跳转到add新增页面
	public String addInputCustomer() {
		cdto = null;
		return "success";
	}

	// List页面跳转到update修改页面
	public String updateInputCustomer() throws Throwable {
		Integer customerId = Integer.valueOf((String) rowlist[0]);
		cdto = customerService.getCustomerById(customerId);
		return "success";
	}

	public String updateCustomer() throws Throwable {
		customerService.updateCustomer(cdto.getCustomer());
		cdto = null;
		return "success";
	}

	public String addCustomer() throws Throwable {
		customerService.addCustomer(cdto.getCustomer());
		cdto = null;
		return "success";
	}

	public String deleteCustomer() throws Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			customerService
					.deleteCustomer(Integer.valueOf((String) rowlist[i]));
		}
		return "success";
	}

	public String selectCustomer() throws Throwable {
		if (verifyprojectCustomer != null) {
			tempcdtos = customerService.getCustomerList(verifyprojectCustomer);
			verifyprojectCustomer = null;
		} else {
			tempcdtos = customerService
					.getCustomerList(new PBaseVerifyprojectCustomer());
		}

		page = PageUtil.generatePageDTO(tempcdtos,
				"showCustomerPageList.action", cpage);
		cdtos = page.getResList();
		return "success";
	}

}
